home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gxstroke.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  26KB  |  761 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxstroke.c */
  20. /* Path stroking procedures for Ghostscript library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gpcheck.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxarith.h"
  27. #include "gxmatrix.h"
  28. #include "gscoord.h"
  29. #include "gzstate.h"
  30. #include "gzdevice.h"
  31. #include "gzcolor.h"            /* requires gxdevice.h */
  32. #include "gzline.h"
  33. #include "gzpath.h"
  34.  
  35. /* Define the filling adjustment that actually produces no adjustment. */
  36. #define fill_no_adjust ((fixed)1)
  37.  
  38. /*
  39.  * Structure for a partial line (passed to the drawing routine).
  40.  * Two of these are required to do joins right.
  41.  * Each endpoint includes the two ends of the cap as well,
  42.  * and the deltas for square and round cap computation.
  43.  *
  44.  * The deltas (co, cdelta, ce) are in clockwise order in device space
  45.  * around the endpoint p: they are one-half the line width (suitably
  46.  * transformed) at 90 degrees counter-clockwise, straight ahead,
  47.  * and 90 degrees clockwise from the oriented line o->e,
  48.  * where "90 degrees" is measured in *user* coordinates.
  49.  * Note that the values at o are the negatives of the values at e.
  50.  *
  51.  * Initially, only o.p, e.p, e.cdelta, width, and thin are set.
  52.  * compute_caps fills in the rest.
  53.  */
  54. typedef gs_fixed_point _ss *p_ptr;
  55. typedef struct endpoint_s {
  56.     gs_fixed_point p;        /* the end of the line */
  57.     gs_fixed_point co, ce;        /* ends of the cap, p +/- width */
  58.     gs_fixed_point cdelta;        /* +/- cap length */
  59. } endpoint;
  60. typedef endpoint _ss *ep_ptr;
  61. typedef struct partial_line_s {
  62.     endpoint o;            /* starting coordinate */
  63.     endpoint e;            /* ending coordinate */
  64.     gs_fixed_point width;        /* one-half line width, see above */
  65.     int thin;            /* true if minimum-width line */
  66. } partial_line;
  67. typedef partial_line _ss *pl_ptr;
  68.  
  69. /* Procedures that stroke a partial_line (the first pl_ptr argument). */
  70. /* If both partial_lines are non-null, the procedure creates */
  71. /* an appropriate join; otherwise, the procedure creates an */
  72. /* end cap.  If the first int is 0, the procedure also starts with */
  73. /* an appropriate cap. */
  74. private int near stroke_add(P5(gx_path *, int, pl_ptr, pl_ptr, gs_state *));
  75. private int near stroke_fill(P5(gx_path *, int, pl_ptr, pl_ptr, gs_state *));
  76.  
  77. /* Other forward declarations */
  78. private int near stroke(P4(const gx_path *, gx_path *,
  79.   int near (*)(P5(gx_path *, int, pl_ptr, pl_ptr, gs_state *)),
  80.   gs_state *));
  81. private void near adjust_stroke(P2(pl_ptr, gs_state *));
  82. private int near expand_dashes(P3(const subpath *, gx_path *, gs_state *));
  83. private void near compute_caps(P1(pl_ptr));
  84. private int near add_capped(P4(gx_path *, gs_line_cap,
  85.   int (*)(P3(gx_path *, fixed, fixed)),
  86.   ep_ptr));
  87.  
  88. /* Stroke a path for drawing or saving */
  89. int
  90. gx_stroke_fill(const gx_path *ppath, gs_state *pgs)
  91. {    return stroke(ppath, (gx_path *)0, stroke_fill, pgs);
  92. }
  93. int
  94. gx_stroke_add(const gx_path *ppath, gx_path *to_path, gs_state *pgs)
  95. {    int code = stroke(ppath, to_path, stroke_add, pgs);
  96.     if ( code < 0 ) return code;
  97.     if ( !ppath->subpath_open && ppath->position_valid )
  98.         code = gx_path_add_point(to_path, ppath->position.x,
  99.                      ppath->position.y);
  100.     return code;
  101. }
  102.  
  103. /* Fill a partial stroked path. */
  104. /* Free variables: code, to_path, ppath, stroke_path_body, pgs, exit (label). */
  105. #define fill_stroke_path()\
  106. if(to_path==&stroke_path_body && !gx_path_is_void_inline(&stroke_path_body))\
  107. { code = gx_fill_path(to_path, pgs->dev_color, pgs, gx_rule_winding_number,\
  108.     fill_no_adjust);\
  109.   gx_path_release(to_path);\
  110.   if ( code < 0 ) goto exit;\
  111.   gx_path_init(to_path, ppath->memory_procs);\
  112. }
  113.  
  114. /* Stroke a path.  Call line_proc (stroke_add or stroke_fill) */
  115. /* for each line segment. */
  116. private int near
  117. stroke(const gx_path *ppath, gx_path *to_path,
  118.   int near (*line_proc)(P5(gx_path *, int, pl_ptr, pl_ptr, gs_state *)),
  119.   gs_state *pgs)
  120. {    const subpath *psub;
  121.     const subpath *save_psub = 0;
  122.     int code = 0;
  123.     const line_params *lp = pgs->line_params;
  124.     int dash_count = lp->dash.pattern_size;
  125.     gx_path fpath, dpath;
  126.     gx_path stroke_path_body;
  127.     float xx = pgs->ctm.xx, xy = pgs->ctm.xy;
  128.     float yx = pgs->ctm.yx, yy = pgs->ctm.yy;
  129.     int skewed = !is_fzero2(xy, yx);
  130.     int uniform = (skewed ? 0 : xx == yy ? 1 : xx == -yy ? -1 : 0);
  131.     /*
  132.      * We are dealing with a reflected coordinate system
  133.      * if (1,0) is counter-clockwise from (0,1).
  134.      * See the note in stroke_add for the algorithm.
  135.      */    
  136.     int reflected =
  137.       (uniform ? uniform > 0 :
  138.        skewed ? xy * yx < xx * yy :
  139.        (xx < 0) == (yy < 0));
  140.     float line_width = lp->width;    /* this is *half* the line width! */
  141.     int always_thin;
  142.     double line_width_and_scale, line_width_scale_xx;
  143. #ifdef DEBUG
  144. if ( gs_debug['o'] )
  145.    {    int count = lp->dash.pattern_size;
  146.     int i;
  147.     dprintf3("[o]half_width=%f, cap=%d, join=%d,\n",
  148.          lp->width, (int)lp->cap, (int)lp->join);
  149.     dprintf2("   miter_limit=%f, miter_check=%f,\n",
  150.          lp->miter_limit, lp->miter_check);
  151.     dprintf1("   dash pattern=%d", count);
  152.     for ( i = 0; i < count; i++ )
  153.         dprintf1(",%f", lp->dash.pattern[i]);
  154.     dprintf4(",\n   offset=%f, init(ink_on=%d, index=%d, dist_left=%f)\n",
  155.          lp->dash.offset, lp->dash.init_ink_on, lp->dash.init_index,
  156.          lp->dash.init_dist_left);
  157.    }
  158. #endif
  159.     if ( line_width < 0 ) line_width = -line_width;
  160.     if ( is_fzero(line_width) )
  161.         always_thin = 1;
  162.     else if ( !skewed )
  163.        {    float xxa = xx, yya = yy;
  164.         if ( xxa < 0 ) xxa = -xxa;
  165.         if ( yya < 0 ) yya = -yya;
  166.         always_thin = (max(xxa, yya) * line_width < 0.5);
  167.        }
  168.     else
  169.        {    /* The check is more complicated, but it's worth it. */
  170.         float xsq = xx * xx + xy * xy;
  171.         float ysq = yx * yx + yy * yy;
  172.         float cross = xx * yx + xy * yy;
  173.         if ( cross < 0 ) cross = 0;
  174.         always_thin =
  175.           ((max(xsq, ysq) + cross) * line_width * line_width < 0.5);
  176.        }
  177.     line_width_and_scale = line_width * (double)int2fixed(1);
  178.     if ( !always_thin && uniform )
  179.     {    /* Precompute a value we'll need later. */
  180.         line_width_scale_xx = line_width_and_scale * xx;
  181.         if ( line_width_scale_xx < 0 )
  182.           line_width_scale_xx = -line_width_scale_xx;
  183.     }
  184.     if_debug5('o', "[o]ctm=(%g,%g,%g,%g) thin=%d\n",
  185.           xx, xy, yx, yy, always_thin);
  186.     /* Start by flattening the path.  We should do this on-the-fly.... */
  187.     if ( !ppath->curve_count )    /* don't need to flatten */
  188.        {    psub = ppath->first_subpath;
  189.         if ( !psub ) return 0;
  190.        }
  191.     else
  192.        {    if ( (code = gx_path_flatten(ppath, &fpath, pgs->flatness, (int)pgs->in_cachedevice)) < 0 )
  193.            return code;
  194.         psub = fpath.first_subpath;
  195.        }
  196.     if ( dash_count )
  197.         gx_path_init(&dpath, ppath->memory_procs);
  198.     if ( to_path == 0 )
  199.     {    /* We might try to defer this if it's expensive.... */
  200.         to_path = &stroke_path_body;
  201.         gx_path_init(to_path, ppath->memory_procs);
  202.     }
  203.     for ( ; ; )
  204.      { const line_segment *pline;
  205.        fixed x, y;
  206.        partial_line pl, pl_prev, pl_first;
  207.        int first = 0;
  208.        int index = 0;
  209.        if ( !psub )
  210.         {    /* Might just be the end of a dash expansion. */
  211.         if ( save_psub )
  212.            {    gx_path_release(&dpath);
  213.             psub = (const subpath *)save_psub->last->next;
  214.             if ( !psub ) break;
  215.             gx_path_init(&dpath, ppath->memory_procs);
  216.             save_psub = 0;
  217.            }
  218.         else        /* all done */
  219.             break;
  220.         }
  221.        if ( dash_count && !save_psub )
  222.         {    code = expand_dashes(psub, &dpath, pgs);
  223.         if ( code < 0 ) goto exit;
  224.         save_psub = (subpath *)psub;
  225.         psub = dpath.first_subpath;
  226.         continue;        /* psub might be null */
  227.         }
  228.        pline = (const line_segment *)(psub->next);
  229.        x = psub->pt.x;
  230.        y = psub->pt.y;
  231.        while ( pline != 0 && pline->type != s_start )
  232.         {    fixed sx = pline->pt.x;
  233.         fixed sy = pline->pt.y;
  234.         /* Compute the width parameters in device space. */
  235.         /* We work with unscaled values, for speed. */
  236.         pl.o.p.x = x, pl.o.p.y = y;
  237.         pl.e.p.x = sx, pl.e.p.y = sy;
  238.         if ( !always_thin )
  239.            {    fixed udx = sx - x, udy = sy - y;
  240.             if ( !(udx | udy) )    /* degenerate */
  241.              { /* Only consider a degenerate segment */
  242.                /* if the entire subpath is degenerate and */
  243.                /* we are using round caps or joins. */
  244.                if ( index != 0 || (pline->next != 0 &&
  245.                  pline->next->type != s_start) ||
  246.                 (lp->cap != gs_cap_round &&
  247.                  lp->join != gs_join_round)
  248.                   )
  249.                  goto nd;
  250.                /* Pick an arbitrary orientation. */
  251.                udx = int2fixed(1);
  252.              }
  253.             if ( uniform != 0 )
  254.                {    /* We can save a lot of work in this case. */
  255.                 float dpx = udx, dpy = udy;
  256.                  float wl = line_width_scale_xx /
  257.                     hypot(dpx, dpy);
  258.                 pl.e.cdelta.x = (fixed)(dpx * wl);
  259.                 pl.e.cdelta.y = (fixed)(dpy * wl);
  260.                 pl.width.x = -pl.e.cdelta.y;
  261.                 pl.width.y = pl.e.cdelta.x;
  262.                 pl.thin = 0;    /* if not always_thin, */
  263.                         /* then never thin. */
  264.                }
  265.             else
  266.                {    gs_point dpt;    /* unscaled */
  267.                 float wl;
  268.                 gs_idtransform_inline(pgs,
  269.                           (float)udx, (float)udy, &dpt);
  270.                 wl = line_width_and_scale /
  271.                     hypot(dpt.x, dpt.y);
  272.                 /* Construct the width vector in */
  273.                 /* user space, still unscaled. */
  274.                 dpt.x *= wl;
  275.                 dpt.y *= wl;
  276.                 /*
  277.                  * We now compute both perpendicular
  278.                  * and (optionally) parallel half-widths,
  279.                  * as deltas in device space.  We use
  280.                  * a fixed-point, unscaled version of
  281.                  * gs_dtransform.  The second computation
  282.                  * folds in a 90-degree rotation (in user
  283.                  * space, before transforming) in the
  284.                  * direction that corresponds to clockwise
  285.                  * in device space.
  286.                  */
  287.                 pl.e.cdelta.x = (fixed)(dpt.x * xx);
  288.                 pl.e.cdelta.y = (fixed)(dpt.y * yy);
  289.                 if ( skewed )
  290.                   pl.e.cdelta.x += (fixed)(dpt.y * yx),
  291.                   pl.e.cdelta.y += (fixed)(dpt.x * xy);
  292.                 if ( reflected )
  293.                   dpt.x = -dpt.x, dpt.y = -dpt.y;
  294.                 pl.width.x = (fixed)(dpt.y * xx),
  295.                 pl.width.y = -(fixed)(dpt.x * yy);
  296.                 if ( skewed )
  297.                   pl.width.x -= (fixed)(dpt.x * yx),
  298.                   pl.width.y += (fixed)(dpt.y * xy);
  299.                 pl.thin =
  300.                   any_abs(pl.width.x) + any_abs(pl.width.y) <
  301.                     float2fixed(0.75);
  302.                }
  303.             if ( !pl.thin )
  304.             {    adjust_stroke(&pl, pgs);
  305.                 compute_caps(&pl);
  306.             }
  307.            }
  308.         else            /* always_thin */
  309.             pl.e.cdelta.x = pl.e.cdelta.y = 0,
  310.             pl.width.x = pl.width.y = 0,
  311.             pl.thin = 1;
  312.         if ( first++ == 0 ) pl_first = pl;
  313.         if ( index++ )
  314.         {    code = (*line_proc)(to_path,
  315.                     (psub->closed ? 1 : index - 2),
  316.                     &pl_prev, &pl, pgs);
  317.             if ( code < 0 ) goto exit;
  318.             fill_stroke_path();
  319.         }
  320.         pl_prev = pl;
  321.         x = sx, y = sy;
  322. nd:        pline = (const line_segment *)(pline->next);
  323.         }
  324.        if ( index )
  325.         {    /* If closed, join back to start, else cap */
  326.         code = (*line_proc)(to_path, index - 1, &pl_prev,
  327.                  (psub->closed ? &pl_first : (pl_ptr)0), pgs);
  328.         if ( code < 0 ) goto exit;
  329.         fill_stroke_path();
  330.         }
  331.        psub = (const subpath *)pline;
  332.      }
  333. exit:    if ( to_path == &stroke_path_body )
  334.         gx_path_release(to_path);    /* (only needed if error) */
  335.     if ( dash_count ) gx_path_release(&dpath);
  336.     if ( ppath->curve_count ) gx_path_release(&fpath);
  337.     return code;
  338. }
  339.  
  340. /* ------ Internal routines ------ */
  341.  
  342. /* Adjust the endpoints and width of a stroke segment */
  343. /* to achieve more uniform rendering. */
  344. /* Only o.p, e.p, e.cdelta, and width have been set. */
  345. private void near
  346. adjust_stroke(pl_ptr plp, gs_state *pgs)
  347. {    fixed _ss *pw;
  348.     fixed _ss *pov;
  349.     fixed _ss *pev;
  350.     fixed w2;
  351.     if ( !pgs->stroke_adjust && plp->width.x != 0 && plp->width.y != 0 )
  352.         return;        /* don't adjust */
  353.     if ( any_abs(plp->width.x) < any_abs(plp->width.y) )
  354.     {    /* More horizontal stroke */
  355.         pw = &plp->width.y, pov = &plp->o.p.y, pev = &plp->e.p.y;
  356.     }
  357.     else
  358.     {    /* More vertical stroke */
  359.         pw = &plp->width.x, pov = &plp->o.p.x, pev = &plp->e.p.x;
  360.     }
  361.     /* Round the larger component of the width up or down, */
  362.     /* whichever way produces a result closer to the correct width. */
  363.     /* Note that just rounding the larger component */
  364.     /* may not produce the correct result. */
  365.     w2 = fixed_rounded(*pw << 1);        /* full line width */
  366.     if ( w2 == 0 && *pw != 0 )
  367.     {    /* Make sure thin lines don't disappear. */
  368.         w2 = (*pw < 0 ? -fixed_1 : fixed_1);
  369.     }
  370.     *pw = arith_rshift_1(w2);
  371.     /* Only adjust the endpoints if the line is horizontal or vertical. */
  372.     if ( *pov == *pev )
  373.     {    if ( w2 & fixed_1 )    /* odd width, move to half-pixel */
  374.         {    *pov = *pev = fixed_floor(*pov) + fixed_half;
  375.         }
  376.         else            /* even width, move to pixel */
  377.         {    *pov = *pev = fixed_rounded(*pov);
  378.         }
  379.     }
  380. }
  381.  
  382. /* Expand a dashed subpath into explicit segments. */
  383. /* The subpath contains no curves. */
  384. private int near
  385. expand_dashes(const subpath *psub, gx_path *ppath, gs_state *pgs)
  386. {    const dash_params *dash = &pgs->line_params->dash;
  387.     const float *pattern = dash->pattern;
  388.     int count, ink_on, index;
  389.     float dist_left;
  390.     fixed x0 = psub->pt.x, y0 = psub->pt.y;
  391.     fixed x, y;
  392.     const segment *pseg;
  393.     int wrap = (dash->init_ink_on && psub->closed ? -1 : 0);
  394.     int drawing = wrap;
  395.     int code;
  396.     if ( (code = gx_path_add_point(ppath, x0, y0)) < 0 )
  397.         return code;
  398.     /* To do the right thing at the beginning of a closed path, */
  399.     /* we have to skip any initial line, and then redo it at */
  400.     /* the end of the path.  Drawing = -1 while skipping, */
  401.     /* 0 while drawing normally, and 1 on the second round. */
  402. top:    count = dash->pattern_size;
  403.     ink_on = dash->init_ink_on;
  404.     index = dash->init_index;
  405.     dist_left = dash->init_dist_left;
  406.     x = x0, y = y0;
  407.     pseg = (const segment *)psub;
  408.     while ( (pseg = pseg->next) != 0 && pseg->type != s_start )
  409.        {    fixed sx = pseg->pt.x, sy = pseg->pt.y;
  410.         fixed udx = sx - x, udy = sy - y;
  411.         float length, dx, dy;
  412.         float dist;
  413.         if ( !(udx | udy) )    /* degenerate */
  414.             dx = 0, dy = 0, length = 0;
  415.         else
  416.            {    gs_point d;
  417.             dx = udx, dy = udy;    /* scaled as fixed */
  418.             gs_idtransform_inline(pgs, dx, dy, &d);
  419.             length = hypot(d.x, d.y) * (1 / (float)int2fixed(1));
  420.            }
  421.         dist = length;
  422.         while ( dist > dist_left )
  423.            {    /* We are using up the dash element */
  424.             float fraction = dist_left / length;
  425.             fixed nx = x + (fixed)(dx * fraction);
  426.             fixed ny = y + (fixed)(dy * fraction);
  427.             if ( ink_on )
  428.                {    if ( drawing >= 0 )
  429.                   code = gx_path_add_line(ppath, nx, ny);
  430.                }
  431.             else
  432.                {    if ( drawing > 0 ) return 0;    /* done */
  433.                 code = gx_path_add_point(ppath, nx, ny);
  434.                 drawing = 0;
  435.                }
  436.             if ( code < 0 ) return code;
  437.             dist -= dist_left;
  438.             ink_on = !ink_on;
  439.             if ( ++index == count ) index = 0;
  440.             dist_left = pattern[index];
  441.             x = nx, y = ny;
  442.            }
  443.         dist_left -= dist;
  444.         /* Handle the last dash of a segment. */
  445.         if ( ink_on )
  446.            {    if ( drawing >= 0 )
  447.               code =
  448.                 (pseg->type == s_line_close && drawing > 0 ?
  449.                  gx_path_close_subpath(ppath) :
  450.                  gx_path_add_line(ppath, sx, sy));
  451.            }
  452.         else
  453.            {    if ( drawing > 0 ) return 0;    /* done */
  454.             code = gx_path_add_point(ppath, sx, sy);
  455.             drawing = 0;
  456.            }
  457.         if ( code < 0 ) return code;
  458.         x = sx, y = sy;
  459.        }
  460.     /* Check for wraparound. */
  461.     if ( wrap && drawing <= 0 )
  462.        {    /* We skipped some initial lines. */
  463.         /* Go back and do them now. */
  464.         drawing = 1;
  465.         goto top;
  466.        }
  467.     return 0;
  468. }
  469.  
  470. /* Compute the intersection of two lines.  This is a messy algorithm */
  471. /* that somehow ought to be useful in more places than just here.... */
  472. /* If the lines are (nearly) parallel, return -1 without setting *pi; */
  473. /* otherwise, return 0 if the intersection is beyond *pp1 and *pp2 in */
  474. /* the direction determined by *pd1 and *pd2, and 1 otherwise. */
  475. private int
  476. line_intersect(
  477.     p_ptr pp1,                /* point on 1st line */
  478.     p_ptr pd1,                /* slope of 1st line (dx,dy) */
  479.     p_ptr pp2,                /* point on 2nd line */
  480.     p_ptr pd2,                /* slope of 2nd line */
  481.     p_ptr pi)                /* return intersection here */
  482. {    /* We don't have to do any scaling, the factors all work out right. */
  483.     float u1 = pd1->x, v1 = pd1->y;
  484.     float u2 = pd2->x, v2 = pd2->y;
  485.     double denom = u1 * v2 - u2 * v1;
  486.     float xdiff = pp2->x - pp1->x;
  487.     float ydiff = pp2->y - pp1->y;
  488.     double f1;
  489.     double max_result = any_abs(denom) * (double)max_fixed;
  490. #ifdef DEBUG
  491. if ( gs_debug['o'] )
  492.    {    dprintf4("[o]Intersect %f,%f(%f/%f)",
  493.          fixed2float(pp1->x), fixed2float(pp1->y),
  494.          fixed2float(pd1->x), fixed2float(pd1->y));
  495.     dprintf4(" & %f,%f(%f/%f),\n",
  496.          fixed2float(pp2->x), fixed2float(pp2->y),
  497.          fixed2float(pd2->x), fixed2float(pd2->y));
  498.     dprintf3("\txdiff=%f ydiff=%f denom=%f ->\n",
  499.          xdiff, ydiff, denom);
  500.    }
  501. #endif
  502.     /* Check for degenerate result. */
  503.     if ( any_abs(xdiff) >= max_result || any_abs(ydiff) >= max_result )
  504.        {    /* The lines are nearly parallel, */
  505.         /* or one of them has zero length.  Punt. */
  506.         if_debug0('o', "\tdegenerate!\n");
  507.         return -1;
  508.        }
  509.     f1 = (v2 * xdiff - u2 * ydiff) / denom;
  510.     pi->x = pp1->x + (fixed)(f1 * u1);
  511.     pi->y = pp1->y + (fixed)(f1 * v1);
  512.     if_debug2('o', "\t%f,%f\n",
  513.           fixed2float(pi->x), fixed2float(pi->y));
  514.     return (f1 >= 0 && (v1 * xdiff >= u1 * ydiff ? denom >= 0 : denom < 0) ? 0 : 1);
  515. }
  516.  
  517. #define lix plp->o.p.x
  518. #define liy plp->o.p.y
  519. #define litox plp->e.p.x
  520. #define litoy plp->e.p.y
  521. #define trsign(v, c) ((v) >= 0 ? (c) : -(c))
  522.  
  523. /* Set up the width and delta parameters for a thin line. */
  524. /* We only approximate the width and height. */
  525. private void near
  526. set_thin_widths(register pl_ptr plp)
  527. {    fixed dx = litox - lix, dy = litoy - liy;
  528.     if ( any_abs(dx) > any_abs(dy) )
  529.     {    plp->width.x = plp->e.cdelta.y = 0;
  530.         plp->width.y = -(plp->e.cdelta.x =
  531.             trsign(dx, -fixed_half));
  532.     }
  533.     else
  534.     {    plp->width.y = plp->e.cdelta.x = 0;
  535.         plp->width.x = -(plp->e.cdelta.y =
  536.             trsign(dy, -fixed_half));
  537.     }
  538. }
  539.  
  540. /* Draw a line on the device. */
  541. private int near
  542. stroke_fill(gx_path *ppath, int first, register pl_ptr plp, pl_ptr nplp,
  543.   gs_state *pgs)
  544. {    if ( plp->thin )
  545.        {    /* Minimum-width line, don't have to be careful. */
  546.         /* We do have to check for the entire line being */
  547.         /* within the clipping rectangle, allowing for some */
  548.         /* slop at the ends. */
  549.         fixed dx = litox - lix, dy = litoy - liy;
  550. #define slop int2fixed(2)
  551.         fixed xslop = trsign(dx, slop);
  552.         fixed yslop = trsign(dy, slop);
  553.         if ( gx_cpath_includes_rectangle(pgs->clip_path,
  554.                 lix - xslop, liy - yslop,
  555.                 litox + xslop, litoy + yslop) )
  556.             return gz_draw_line_fixed(lix, liy, litox, litoy,
  557.                 pgs->dev_color, pgs);
  558. #undef slop
  559. #undef trsign
  560.         /* We didn't set up the endpoint parameters before, */
  561.         /* because the line was thin.  stroke_add will do this. */
  562.        }
  563.     /* General case: construct a path for the fill algorithm. */
  564.     return stroke_add(ppath, first, plp, nplp, pgs);
  565. }
  566.  
  567. #undef lix
  568. #undef liy
  569. #undef litox
  570. #undef litoy
  571.  
  572. /* Add a segment to the path.  This handles all the complex cases. */
  573. private int near add_capped(P4(gx_path *, gs_line_cap, int (*)(P3(gx_path *, fixed, fixed)), ep_ptr));
  574. private int near
  575. stroke_add(gx_path *ppath, int first, register pl_ptr plp, pl_ptr nplp,
  576.   gs_state *pgs)
  577. {    int code;
  578.     if ( plp->thin )
  579.        {    /* We didn't set up the endpoint parameters before, */
  580.         /* because the line was thin.  Do it now. */
  581.         set_thin_widths(plp);
  582.         adjust_stroke(plp, pgs);
  583.         compute_caps(plp);
  584.        }
  585.     if ( (code = add_capped(ppath, (first == 0 ? pgs->line_params->cap : gs_cap_butt), gx_path_add_point, &plp->o)) < 0 )
  586.         return code;
  587.     if ( nplp == 0 )
  588.        {    code = add_capped(ppath, pgs->line_params->cap, gx_path_add_line, &plp->e);
  589.        }
  590.     else if ( pgs->line_params->join == gs_join_round )
  591.        {    code = add_capped(ppath, gs_cap_round, gx_path_add_line, &plp->e);
  592.        }
  593.     else if ( nplp->thin )        /* no join */
  594.       {    code = add_capped(ppath, gs_cap_butt, gx_path_add_line, &plp->e);
  595.       }
  596.     else                /* join_bevel or join_miter */
  597.        {    gs_fixed_point jp1, jp2;
  598.         /*
  599.          * Set np to whichever of nplp->o.co or .ce
  600.          * is outside the current line.  We observe that
  601.          * point (x2,y2) is counter-clockwise from (x1,y1)
  602.          * relative to the origin iff x1*y2 < x2*y1.
  603.          * In this case x1,y1 are plp->width,
  604.          * x2,y2 are nplp->width, and the origin is
  605.          * their common point (plp->e.p, nplp->o.p).
  606.          */
  607.         float wx1 = plp->width.x, wy1 = plp->width.y;
  608.         float wx2 = nplp->width.x, wy2 = nplp->width.y;
  609.         int ccw = wx1 * wy2 < wx2 * wy1;
  610.         p_ptr outp, np, np1, np2;
  611.         /* Initialize for a bevel join. */
  612.         jp1.x = plp->e.co.x, jp1.y = plp->e.co.y;
  613.         jp2.x = plp->e.ce.x, jp2.y = plp->e.ce.y;
  614.         if ( ccw )
  615.             outp = &jp2, np2 = np = &nplp->o.co, np1 = &plp->e.p;
  616.         else
  617.             outp = &jp1, np1 = np = &nplp->o.ce, np2 = &plp->e.p;
  618.         if_debug1('o', "[o]use %s\n", (ccw ? "co (ccw)" : "ce (cw)"));
  619.         /* Don't bother with the miter check if the two */
  620.         /* points to be joined are very close together, */
  621.         /* namely, in the same square half-pixel. */
  622.         if ( pgs->line_params->join == gs_join_miter &&
  623.              !(fixed2long(outp->x << 1) == fixed2long(np->x << 1) &&
  624.                fixed2long(outp->y << 1) == fixed2long(np->y << 1))
  625.            )
  626.           { /*
  627.              * Check whether a miter join is appropriate.
  628.              * Let a, b be the angles of the two lines.
  629.              * We check tan(a-b) against the miter_check
  630.              * by using the following formula:
  631.              * If tan(a)=u1/v1 and tan(b)=u2/v2, then
  632.              * tan(a-b) = (u1*v2 - u2*v1) / (u1*u2 + v1*v2).
  633.              * We can do all the computations unscaled,
  634.              * because we're only concerned with ratios.
  635.              */
  636.             float u1 = plp->e.cdelta.x, v1 = plp->e.cdelta.y;
  637.             float u2 = nplp->o.cdelta.x, v2 = nplp->o.cdelta.y;
  638.             double num = u1 * v2 - u2 * v1;
  639.             double denom = u1 * u2 + v1 * v2;
  640.             float check = pgs->line_params->miter_check;
  641.             /*
  642.              * We will want either tan(a-b) or tan(b-a)
  643.              * depending on the orientations of the lines.
  644.              * Fortunately we know the relative orientations already.
  645.              */
  646.             if ( !ccw )        /* have plp - nplp, want vice versa */
  647.             num = -num;
  648. #ifdef DEBUG
  649. if ( gs_debug['o'] )
  650.                    {    dprintf4("[o]Miter check: u1/v1=%f/%f, u2/v2=%f/%f,\n",
  651.                  u1, v1, u2, v2);
  652.                         dprintf3("        num=%f, denom=%f, check=%f\n",
  653.                  num, denom, check);
  654.                    }
  655. #endif
  656.             /* Use a miter if num / denom >= check. */
  657.             /* If check > 0, num < 0 always passes; */
  658.             /* if check < 0, num >= 0 always fails. */
  659.             if ( denom < 0 ) num = -num, denom = -denom;
  660.             if ( check > 0 ?
  661.             (num < 0 || num >= denom * check) :
  662.             (num < 0 && num >= denom * check)
  663.                )
  664.             {    /* OK to use a miter join. */
  665.             gs_fixed_point mpt;
  666.             if_debug0('o', "        ... passes.\n");
  667.             /* Compute the intersection of */
  668.             /* the extended edge lines. */
  669.             if ( line_intersect(outp, &plp->e.cdelta, np,
  670.                         &nplp->o.cdelta, &mpt) == 0
  671.                )
  672.                 outp->x = mpt.x,
  673.                 outp->y = mpt.y;
  674.             }
  675.            }
  676.         if ( (code = gx_path_add_line(ppath, jp1.x, jp1.y)) < 0 ||
  677.              (code = gx_path_add_line(ppath, np1->x, np1->y)) < 0 ||
  678.              (code = gx_path_add_line(ppath, np2->x, np2->y)) < 0 ||
  679.              (code = gx_path_add_line(ppath, jp2.x, jp2.y)) < 0
  680.            )
  681.             return code;
  682.        }
  683.     if ( code < 0 || (code = gx_path_close_subpath(ppath)) < 0 )
  684.         return code;
  685.     return 0;
  686. }
  687.  
  688. /* Routines for cap computations */
  689.  
  690. /* Compute the endpoints of the two caps of a segment. */
  691. private void near
  692. compute_caps(register pl_ptr plp)
  693. {    fixed wx2 = plp->width.x;
  694.     fixed wy2 = plp->width.y;
  695.     plp->o.co.x = plp->o.p.x + wx2, plp->o.co.y = plp->o.p.y + wy2;
  696.     plp->o.cdelta.x = -plp->e.cdelta.x,
  697.       plp->o.cdelta.y = -plp->e.cdelta.y;
  698.     plp->o.ce.x = plp->o.p.x - wx2, plp->o.ce.y = plp->o.p.y - wy2;
  699.     plp->e.co.x = plp->e.p.x - wx2, plp->e.co.y = plp->e.p.y - wy2;
  700.     plp->e.ce.x = plp->e.p.x + wx2, plp->e.ce.y = plp->e.p.y + wy2;
  701. #ifdef DEBUG
  702. if ( gs_debug['o'] )
  703.     dprintf4("[o]Stroke o=(%f,%f) e=(%f,%f)\n",
  704.          fixed2float(plp->o.p.x), fixed2float(plp->o.p.y),
  705.          fixed2float(plp->e.p.x), fixed2float(plp->e.p.y)),
  706.     dprintf4("\twxy=(%f,%f) lxy=(%f,%f)\n",
  707.          fixed2float(wx2), fixed2float(wy2),
  708.          fixed2float(plp->e.cdelta.x), fixed2float(plp->e.cdelta.y));
  709. #endif
  710. }
  711.  
  712. /* Add a properly capped line endpoint to the path. */
  713. /* The first point may require either moveto or lineto. */
  714. private int near
  715. add_capped(gx_path *ppath, gs_line_cap type,
  716.   int (*add_proc)(P3(gx_path *, fixed, fixed)), /* gx_path_add_point/line */
  717.   register ep_ptr endp)
  718. {    int code;
  719. #define px endp->p.x
  720. #define py endp->p.y
  721. #define xo endp->co.x
  722. #define yo endp->co.y
  723. #define xe endp->ce.x
  724. #define ye endp->ce.y
  725. #define cdx endp->cdelta.x
  726. #define cdy endp->cdelta.y
  727. #ifdef DEBUG
  728. if ( gs_debug['o'] )
  729.     dprintf4("[o]cap: p=(%g,%g), co=(%g,%g),\n",
  730.          fixed2float(px), fixed2float(py),
  731.          fixed2float(xo), fixed2float(yo)),
  732.     dprintf4("[o]\tce=(%g,%g), cd=(%g,%g)\n",
  733.          fixed2float(xe), fixed2float(ye),
  734.          fixed2float(cdx), fixed2float(cdy));
  735. #endif
  736.     switch ( type )
  737.        {
  738.     case gs_cap_round:
  739.        {    fixed xm = px + cdx;
  740.         fixed ym = py + cdy;
  741.         if (    (code = (*add_proc)(ppath, xo, yo)) < 0 ||
  742.             (code = gx_path_add_arc(ppath, xo, yo, xm, ym,
  743.                 xo + cdx, yo + cdy, quarter_arc_fraction)) < 0 ||
  744.             (code = gx_path_add_arc(ppath, xm, ym, xe, ye,
  745.                 xe + cdx, ye + cdy, quarter_arc_fraction)) < 0
  746.            ) return code;
  747.        }
  748.         break;
  749.     case gs_cap_square:
  750.         if (    (code = (*add_proc)(ppath, xo + cdx, yo + cdy)) < 0 ||
  751.             (code = gx_path_add_line(ppath, xe + cdx, ye + cdy)) < 0
  752.            ) return code;
  753.         break;
  754.     case gs_cap_butt:
  755.         if (    (code = (*add_proc)(ppath, xo, yo)) < 0 ||
  756.             (code = gx_path_add_line(ppath, xe, ye)) < 0
  757.            ) return code;
  758.        }
  759.     return code;
  760. }
  761.